package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.LendRecord;
import com.example.demo.mapper.LendRecordMapper;
import com.example.demo.service.LendRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class LendRecordServiceImpl implements LendRecordService {

    @Resource
    private LendRecordMapper lendRecordMapper;

    @Override
    public boolean save(LendRecord record) {
        return lendRecordMapper.insert(record) > 0;
    }

    @Override
    public boolean update(LendRecord record) {
        return lendRecordMapper.updateById(record) > 0;
    }

    @Override
    public boolean deleteBatch(List<Integer> ids) {
        return lendRecordMapper.deleteBatchIds(ids) >= 0;
    }

    @Override
    public boolean deleteById(Long id) {
        return lendRecordMapper.deleteById(id) > 0;
    }

    @Override
    public Page<LendRecord> findPage(Integer pageNum, Integer pageSize, String search) {
        LambdaQueryWrapper<LendRecord> wrapper = Wrappers.<LendRecord>lambdaQuery();
        // 可以根据实际需求添加搜索条件
        if (StringUtils.isNotBlank(search)) {
            // 假设借阅记录可以按读者ID或ISBN搜索
            try {
                Integer searchId = Integer.parseInt(search);
                wrapper.eq(LendRecord::getReaderId, searchId);
            } catch (NumberFormatException e) {
                // 如果不是数字，则按ISBN或书名搜索
                wrapper.like(LendRecord::getIsbn, search)
                       .or()
                       .like(LendRecord::getBookname, search);
            }
        }
        return lendRecordMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
    }
} 